package com.shuda.business.user.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.shuda.business.user.entity.MenuEntity;
import com.shuda.business.user.service.MenuService;
import com.shuda.common.result.Result;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import jakarta.validation.Valid;
import java.util.List;

/**
 * 菜单管理控制器类
 */
@RestController
@RequestMapping("/api/system/menu")
@Tag(name = "系统菜单管理", description = "系统菜单管理相关接口")
@RequiredArgsConstructor
@Validated
public class MenuController {

    private final MenuService menuService;

    /**
     * 获取菜单树
     */
    @GetMapping("/tree")
    @Operation(summary = "获取菜单树")
    public Result<List<MenuEntity>> getMenuTree() {
        List<MenuEntity> menuTree = menuService.getMenuTree();
        return Result.success(menuTree);
    }

    /**
     * 获取所有菜单列表
     */
    @GetMapping("/list")
    @Operation(summary = "获取所有菜单列表")
    public Result<List<MenuEntity>> getMenuList(@RequestParam(required = false) String menuName,
                                              @RequestParam(required = false) String status) {
        QueryWrapper<MenuEntity> queryWrapper = new QueryWrapper<>();
        if (menuName != null && !menuName.trim().isEmpty()) {
            queryWrapper.like("menu_name", menuName);
        }
        if (status != null && !status.trim().isEmpty()) {
            queryWrapper.eq("status", status);
        }
        queryWrapper.orderByAsc("sort_order");
        
        List<MenuEntity> menuList = menuService.getMenuList(queryWrapper);
        return Result.success(menuList);
    }

    /**
     * 获取菜单详情
     */
    @GetMapping("/{id}")
    @Operation(summary = "获取菜单详情")
    public Result<MenuEntity> getMenuById(@PathVariable Long id) {
        MenuEntity menu = menuService.getById(id);
        return Result.success(menu);
    }

    /**
     * 创建菜单
     */
    @PostMapping
    @Operation(summary = "创建菜单")
    public Result<String> createMenu(@Valid @RequestBody MenuEntity menu) {
        menuService.createMenu(menu);
        return Result.success("菜单创建成功");
    }

    /**
     * 更新菜单
     */
    @PutMapping("/{id}")
    @Operation(summary = "更新菜单")
    public Result<String> updateMenu(@PathVariable Long id, @Valid @RequestBody MenuEntity menu) {
        menu.setId(id);
        menuService.updateMenu(menu);
        return Result.success("菜单更新成功");
    }

    /**
     * 删除菜单
     */
    @DeleteMapping("/{id}")
    @Operation(summary = "删除菜单")
    public Result<String> deleteMenu(@PathVariable Long id) {
        menuService.deleteMenu(id);
        return Result.success("菜单删除成功");
    }

    /**
     * 更新菜单状态
     */
    @PutMapping("/{id}/status")
    @Operation(summary = "更新菜单状态")
    public Result<String> updateMenuStatus(@PathVariable Long id, @RequestParam String status) {
        menuService.updateMenuStatus(id, status);
        return Result.success("菜单状态更新成功");
    }

    /**
     * 更新菜单排序
     */
    @PutMapping("/sort")
    @Operation(summary = "更新菜单排序")
    public Result<String> updateMenuSort(@RequestBody List<MenuEntity> menus) {
        menuService.updateMenuSort(menus);
        return Result.success("菜单排序更新成功");
    }
}